import axios from 'axios'
import { message } from 'antd'
import { GetUserToken } from '../utils'
import {} from 'react-router-dom'

const instance = axios.create({
    timeout: 60000,
    baseURL:'http://localhost:9999/'
})

instance.interceptors.request.use((config)=>{
    const token = GetUserToken()
    if(!token){
        message.error('重新登陆')
    }
    config.headers["Authorization"] = `Bearer ${token}`;
    return config
},err =>{
    return Promise.reject(err)
})

instance.interceptors.response.use((response) => {
    const resdata = (response.data || {}) as responseType
    const { error, data, msg } = resdata;
    if(error !== 0){
        msg ? message.error(msg) :  message.error('网络错误')
    }
    return data as any
},err => {
    message.error('网络错误')
})

type responseType = {
    error: number,
    data: dataType,
    msg?:string
}

type dataType = {
    [key: string]: any
}

export default instance